package com.eairlv.share.auto.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eairlv.share.auto.entity.Result;
import com.eairlv.share.auto.entity.bo.ShareTableDbForm;
import com.eairlv.share.auto.entity.bo.ShareTableSearchForm;
import com.eairlv.share.auto.entity.po.ShareTableDbPO;
import com.eairlv.share.auto.entity.po.ShareTablePO;
import com.eairlv.share.auto.entity.vo.ShareTableDbVO;
import com.eairlv.share.auto.mapper.ShareTableDbMapper;
import com.eairlv.share.auto.utils.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author eairlv
 * @description
 * @date 11:23 2020/1/16
 */
@Slf4j
@Service
public class ShareTableDbService {

    @Autowired
    ShareTableDbMapper shareTableDbMapper;

    @Autowired
    ShareTableService shareTableService;

    /**
     * 查询分片规则数据库
     * @param shareTableSearchForm
     * @return
     */
    public Result<List<ShareTableDbVO>> getShareDb(ShareTableSearchForm shareTableSearchForm) {
        ShareTablePO shareTablePO;
        if (shareTableSearchForm.getShareTableId() != null){
            shareTablePO = shareTableService.getShareById(shareTableSearchForm.getShareTableId());
        } else if(StrUtil.isNotBlank(shareTableSearchForm.getTemplateTableName())){
            shareTablePO = shareTableService.getShareByTemplateTableName(shareTableSearchForm.getTemplateTableName());
        } else {
            return Result.failMessage("parameters must be passed to one of shareTableId or templateTableName");
        }
        if (shareTablePO != null){
            return Result.ok(getShareDbByShareTableId(shareTablePO.getId())
                    .parallelStream()
                    .map(e -> {
                        ShareTableDbVO shareTableDbVO = BeanUtil.copyProperties(e, ShareTableDbVO.class);
                        shareTableDbVO.setTemplateTableName(shareTablePO.getTemplateTableName());
                        return shareTableDbVO;})
                    .collect(Collectors.toList()));
        } else {
            return Result.ok(new ArrayList<>());
        }
    }

    /**
     * 绑定分片数据库
     * @param shareTableDbForm
     * @return
     */
    public Result<Integer> addShareDb(ShareTableDbForm shareTableDbForm) {
        ShareTableDbPO shareTableDbPO = BeanUtil.copyProperties(shareTableDbForm, ShareTableDbPO.class);
        Result<Integer> result = checkShareDb(shareTableDbPO);
        if (result == null){
            if (shareTableDbForm.getId() == null){
                shareTableDbPO.setCreateTime(System.currentTimeMillis());
                insert(shareTableDbPO);
            } else {
                shareTableDbPO.setModifyTime(System.currentTimeMillis());
                update(shareTableDbPO);
            }
            return Result.ok(shareTableDbPO.getId());
        } else {
            return result;
        }
    }

    /**
     * 检查分片数据库
     * @param shareTableDbPO
     * @return
     */
    private Result<Integer> checkShareDb(ShareTableDbPO shareTableDbPO){
        if (shareTableDbPO.getShareTableId() != null && shareTableService.getShareById(shareTableDbPO.getShareTableId()) != null){
            if (StrUtil.isNotBlank(shareTableDbPO.getUrl()) && StrUtil.isNotBlank(shareTableDbPO.getUsername())){
                return null;
            } else {
                return Result.failMessage("share table db must used url and username");
            }
        } else {
            return Result.failMessage("not exist this share table id :" + shareTableDbPO.getShareTableId());
        }
    }

    /**
     * 删除分片数据库
     * @param id
     * @return
     */
    public Result<Integer> deleteShareDb(Integer id) {
        return Result.ok(delete(id));
    }

    /**
     * 插入分片规则数据库
     * @param shareTableDbPO
     * @return
     */
    public Integer insert(ShareTableDbPO shareTableDbPO){
        shareTableDbMapper.insert(shareTableDbPO);
        return shareTableDbPO.getId();
    }

    /**
     * 更新分片规则数据库
     * @param shareTableDbPO
     * @return
     */
    public Integer update(ShareTableDbPO shareTableDbPO){
        shareTableDbMapper.updateById(shareTableDbPO);
        return shareTableDbPO.getId();
    }

    /**
     * 删除分片规则数据库
     * @param id
     * @return
     */
    public Integer delete(Integer id){
        shareTableDbMapper.deleteById(id);
        return id;
    }

    /**
     * 通过shareTableId查询分片规则数据库
     * @param shareTableId
     * @return
     */
    public List<ShareTableDbPO> getShareDbByShareTableId(Integer shareTableId){
        QueryWrapper<ShareTableDbPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("share_table_id", shareTableId);
        return shareTableDbMapper.selectList(queryWrapper);
    }
}
